<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var limitObj=new Object();//用于保存word正文控制大小信息对象  limitObj.num限制大小  limitObj.str限制大小对应提示文字
limitObj.num=10485760;//默认10M
limitObj.str="10M";//默认10M
$(function(){
	
	
	//表格加载
	$('#dispatchListTable').datagrid({
	    url:'<c:url value="/docDispatch/dispatchList" />',
	    queryParams:getParams(),//提交参数
	    
	    toolbar:'#dispatchListToolbar',//表头工具栏
	    pagination:true,//显示分页  
	    pagePosition:'bottom',//分页栏出现位置
	    pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    nowrap: false,//设置内容多时换行，false换行
//	    singleSelect:true,
	    columns:[[
			{field:'docId',title:'序号',width:50,formatter: function(value,row,index){
				return (index+1);
			}},
			{field:'docName',title:'发文标题',width:400,formatter: function(value,row,index){
				return "<a href='javascript:openWord(\""+row.docId+"\",\""+row.formId+"\")'>"+value+"</a>";
			}},
			{field:'displayNumber',title:'文号',width:180},
			{field:'draftOrg',title:'发文单位',width:150},
			{field:'sendTime',title:'发文时间',width:100,formatter: function(value,row,index){
				return value==""?"":dataFormatStr(new Date(value),"yyyy-MM-dd");
			}},
			{field:'formId',title:'附件',width:100,formatter: function(value,row,index){
				return "<a href='javascript:_openAttachsWindow(\""+row.docId+"\")'>查阅附件</a>";
			}},
			
	    ]]
	});
	
	//查询按钮绑定点击事件
	$("#searchButton").click(function(){ 
		$('#dispatchListTable').datagrid({ queryParams:getParams()});
	});
});

function getParams(){
	var jsonObj=new Object();
	if($('input[name="docName"]').val()!=""){
		jsonObj.docName=$('input[name="docName"]').val();
	}
	if($('input[name="displayNumber"]').val()!=""){
		jsonObj.displayNumber=$('input[name="displayNumber"]').val();
	}
	if($('input[name="sendTimeStart"]').val()!=""){
		jsonObj.sendTimeStart=$('input[name="sendTimeStart"]').val();
	}
	if($('input[name="sendTimeEnd"]').val()!=""){
		jsonObj.sendTimeEnd=$('input[name="sendTimeEnd"]').val();
	}
	if($('input[name="dispatchOrg"]').val()!=""){
		jsonObj.dispatchOrg=$('input[name="dispatchOrg"]').val();
	}
	return jsonObj;
}

var asAttachList = [];
var arAttachList = [];
function _openAttachsWindow(issueId){
	var attachsDivId = "#attachsDiv";
	var $getAttachs = $.ajax({
		type: "POST",
		url: _synergyContext+"/basic/synAttach/findAttachsByDocId",
		data:"docId=" + issueId,// + "&timeASC=true&numberASC=true",
		async: false,
		success: function(json) {
			// 清空加载数据的提示信息
		  	$("#attachsDiv").find('#viewAttachs').empty();
		  	// 取得节点列表
			var _cpObjectAS = json.asAttachs;
			var _cpObjectAR = json.arAttachs;
			asAttachList = json.asAttachs;
			arAttachList = json.arAttachs;
			if(_cpObjectAS != '' && typeof(_cpObjectAS) != 'undefined') {
				$(attachsDivId).find('#viewAttachs').append('<tr><td style="padding-left:10px;padding-right:0px;height:20px">正文附件：</td></tr>');
				for(var i = 0 ; i < _cpObjectAS.length; i++) {
					if(_cpObjectAS[i].attachId) {
						_createAttachsTr(attachsDivId, _cpObjectAS[i].attachId,_cpObjectAS[i].attachName,_cpObjectAS[i].suffixName, i+1);
					}
				}
			}
			if(_cpObjectAR != '' && typeof(_cpObjectAR) != 'undefined') {
				$(attachsDivId).find('#viewAttachs').append('<tr><td style="padding-left:10px;padding-right:0px;height:20px">参考附件：</td></tr>');
				for(var i = 0 ; i < _cpObjectAR.length; i++) {
					if(_cpObjectAR[i].attachId) {
						_createAttachsTr(attachsDivId, _cpObjectAR[i].attachId,_cpObjectAR[i].attachName,_cpObjectAR[i].suffixName, i+1);
					}
				}
			}
		
			if((_cpObjectAS != '' && typeof(_cpObjectAS) != 'undefined') || (_cpObjectAR != '' && typeof(_cpObjectAR) != 'undefined')) {
				openAllAttachs();
			} else {
				$.messager.alert("提示","该发文无附件");
			}
		}
	});
}
function _createAttachsTr(attachsDivId, attachId, attachName,suffixName, countNo) {
	var attachHtml = "<a href='javascript:;' onclick='openAttach(\"" + attachId + "\")'>" + countNo + "、" + attachName + suffixName + "</a>";
	var attach = $('<tr><td style="padding-left:40px;padding-right:0px;height:20px">' + attachHtml + '</td></tr>');
	$(attachsDivId).find('#viewAttachs').append(attach);
}
function openAllAttachs() {
	var attachDivId = '#attachsDiv';
	var attahcDivTitle = "附件列表";
	$(attachDivId).dialog({
		title: attahcDivTitle,
		width:500,
		height:400,
		buttons: {
			关闭: function(){
				$(this).dialog( "close" );
			}
		}
	})
}
function openAttach(attachId) {
	for(var key in asAttachList){
    	var attachTmp = asAttachList[key];
    	if(attachTmp.attachId == attachId) {
    		var attachUrl = _synergyContext+"/basic/office/gotoAttachOfficePage";
    		if(attachTmp.suffixName.toLowerCase() == '.doc' || attachTmp.suffixName.toLowerCase() == '.docx' || attachTmp.suffixName.toLowerCase() == '.ppt' 
    				|| attachTmp.suffixName.toLowerCase() == '.pptx' || attachTmp.suffixName.toLowerCase() == '.xls' || attachTmp.suffixName.toLowerCase() == '.xlsx' || attachTmp.suffixName.toLowerCase() == '.pdf'){
				var viewName = attachTmp.attachName + attachTmp.suffixName;
				var vHeight = window.screen.height -73 ;						
				attachUrl = attachUrl +  "?attachId="+attachTmp.attachId +"&suffixName="+ attachTmp.suffixName + "&docId"+attachTmp.docId+"&newFlag=0&fileReadOnly=true&notkHeigth="+vHeight;
				var winFeatures = "height="+vHeight+"px,width="+(window.screen.width-20)+"px,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
				window.open(attachUrl,'_blank',winFeatures);
			}else{
				window.location.href =_synergyContext+"/basic/synAttach/downLoadFile" + "?attachId=" + attachTmp.attachId + "&attachName="+encodeURIComponent(encodeURIComponent(attachTmp.attachName))+"&suffixName="+attachTmp.suffixName+"&newFlag=0";
			}
			break;
    	}
	}
	for(var key in arAttachList){
    	var attachTmp = arAttachList[key];
    	if(attachTmp.attachId == attachId) {
    		var attachUrl = _synergyContext+"/basic/office/gotoAttachOfficePage";
    		if(attachTmp.suffixName.toLowerCase() == '.doc' || attachTmp.suffixName.toLowerCase() == '.docx' || attachTmp.suffixName.toLowerCase() == '.ppt' 
    				|| attachTmp.suffixName.toLowerCase() == '.pptx' || attachTmp.suffixName.toLowerCase() == '.xls' || attachTmp.suffixName.toLowerCase() == '.xlsx' || attachTmp.suffixName.toLowerCase() == '.pdf'){
				var viewName = attachTmp.attachName + attachTmp.suffixName;
				var vHeight = window.screen.height -73 ;	
				// writeAble:在线修改附件，ture：是，false：否					
				attachUrl = attachUrl +  "?attachId="+attachTmp.attachId +"&suffixName="+ attachTmp.suffixName + "&docId"+attachTmp.docId+"&newFlag=0&fileReadOnly=true&notkHeigth="+vHeight;
				var winFeatures = "height="+vHeight+"px,width="+(window.screen.width-20)+"px,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
				window.open(attachUrl,'_blank',winFeatures);
			}else{
				window.location.href =_synergyContext+"/basic/synAttach/downLoadFile" + "?attachId=" + attachTmp.attachId + "&attachName="+encodeURIComponent(encodeURIComponent(attachTmp.attachName))+"&suffixName="+attachTmp.suffixName+"&newFlag=0";
			}
			break;
    	}
	}
}
function openWord(issueId,formId){
	var vHeight = window.screen.height -80 ;
	var fileReadOnly=true;//只读(查看正文)
	var clearDoc=false;//清稿
	var mergeDoc=false;//套红
	var signDoc=false;//签章
	var printFlag=false;//打印
	

	var wordUrl =_synergyContext+"/basic/office/editOffice" + "?docId=" +issueId + "&formId="+formId+"&docType=done"
	+"&clearDocFlag="+clearDoc+"&mergeDocFlag="+mergeDoc+"&signDocFlag="+signDoc+"&printFlag="+printFlag
	+"&fileReadOnly="+fileReadOnly+"&notkHeigth="+vHeight+"&limitNum="+limitObj.num+"&limitStr="+limitObj.str;
//	var winFeatures = "height="+vHeight+"px,width="+(window.screen.width-20)+"px,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
//	window.open(wordUrl,'_blank',winFeatures);
	 ntkoBrowser.openWindow(wordUrl);
}